package com.ccc.orm.entity;

import com.ccc.orm.data.dao.OrmDao;
import com.ccc.orm.data.service.OrmService;
import com.ccc.orm.data.service.QueryService;

import java.util.List;

/**
 * orm 模型访问对象, POJO继承该类便成为一个Modle 拥有DAO的能力
 *
 * @author charry
 * @version 0.1
 * @since 2019/6/30 19:18
 */
public class OrmModel<T> extends OrmDao<T> implements OrmService<T> {

    @Override
    public void insert() {
        insert(this);
    }

    @Override
    public void update() {
        update(this);
    }

    @Override
    public void delete() {
        delete(this);
    }

    private final String tableNmae = entityClass.getSimpleName().toLowerCase();

    @Override
    public QueryService<T> createQuery() {
        return new QueryService<T>() {
            @Override
            public List<T> list() {
                String sql = "select * from "+tableNmae;
                return (List<T>) query(sql, new Object[]{});
            }

            @Override
            public List<T> page(int page, int size) {
                return null;
            }

            @Override
            public T findBy(String column, Object value) {
                String sql = "select * from "+ tableNmae +" where "+column+" = "+"?";
                return (T) query(sql, value).get(0);
            }
        };
    }
}
